package cn.xx.ai4.controller;

import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.splitter.DocumentByLineSplitter;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class RagController {

    //向量存储
    @Autowired
    private EmbeddingStore<TextSegment> embeddingStore;

    //向量模型
    private EmbeddingModel embeddingModel;

    /**
     * 加载文档，创建RAG知识库
     */
    @RequestMapping("/rag/load")
    public void ragLoad() {
        List<Document> documents = FileSystemDocumentLoader.loadDocuments("C:\\Users\\Administrator\\Desktop\\rag");
        EmbeddingStoreIngestor.builder()
                .embeddingStore(embeddingStore)
                .embeddingModel(embeddingModel)
                //文档切割 - 按照什么样的规则把文档分段
                .documentSplitter(new DocumentByLineSplitter(100,10))
                .build().ingest(documents);
    }

}
